package top.linyuxb.lycloud.common.properties;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/8/14 14:33
 * @Desc:
 */
@Getter
@Setter
@ToString
@ConfigurationProperties(prefix = "ly-gateway")
public class LyGatewayProperties {
    /**
     * 是否开启，默认关闭
     */
    private Boolean open = Boolean.FALSE;
    /**
     * 跨域配置
     */
    private LyGatewayCorsConfigProperties corsConfig;
    /**
     * aad登入配置
     */
    private LyGatewayAadProperties aad;
    /**
     * 链路tLog
     */
    private LyGatewayTLogProperties tLog;
    /**
     * 网关路由
     */
    private LyGatewayRouterProperties router;

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:36
     * @Desc: 请求跨域
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayCorsConfigProperties {
        /**
         * 是否开启，默认关闭
         */
        private Boolean open = Boolean.FALSE;
        /**
         * 允许哪些网站的跨域请求 全部配置：*，指定多个逗号分割
         */
        private String allowedOrigins = "*";
        /**
         * 允许在请求中携带的头信息 全部配置：*，指定多个逗号分割
         */
        private String allowedHeaders = "*";
        /**
         * 允许的跨域ajax的请求方式 全部配置：*，指定多个逗号分割
         */
        private String allowedMethods = "*";
        /**
         * 允许在请求中携带cookie
         */
        private Boolean allowCredentials = Boolean.TRUE;
        /**
         * 本次跨域检测的有效期(单位毫秒)
         */
        private Long maxAge;
    }

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:36
     * @Desc: aad登入
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayAadProperties {
        /**
         * 是否开启，默认关闭
         */
        private Boolean open = Boolean.FALSE;
        /**
         * aad jwt在请求header中的key
         */
        private String jwtKey;
        /**
         * 过滤url集合，逗号分割，该路由不允许访问
         */
        private String filterUrls = "";
        /**
         * 允许url集合，逗号分割，该路由不受登入管控，直接访问
         */
        private String allowUrls = "";
        /**
         * 应用appId
         */
        private String appId;
        /**
         * 营养tid
         */
        private String tid;
        /**
         * 微软公钥地址
         */
        private String configUrl;
    }

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:55
     * @Desc: 链路tLog
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayTLogProperties {
        /**
         * 是否开启，默认关闭
         */
        private Boolean open = Boolean.FALSE;
        /**
         * 日志id
         */
        private String traceId = "trackId";
    }

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:57
     * @Desc: 网关路由，网关请求默认开头：/lyGateway
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayRouterProperties {
        /**
         * 是否开启，默认关闭
         */
        private Boolean open = Boolean.FALSE;
        /**
         * 请求前缀
         */
        private String prefix = "/lyGateway";
        /**
         * 路由配置
         */
        private List<LyGatewayRouter> routers;
    }

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:55
     * @Desc: 路由配置
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayRouter {
        /**
         * 唯一标识，无含义
         */
        private String id;
        /**
         * 目标服务配置：
         * 1、lb://app:env 注册中心服务名 注册中心服务
         * 2、http:// 或 https:// 不需要配具体路由，更具网关请求路由自动追加
         */
        private String uri;
        /**
         * 负载均衡类型，当使用注册中心获取服务是，改参数可调试负载均衡策略
         * 支持：roundRobin、weightRobin、random、weightRandom、ipHash
         */
        private String ribbon = "roundRobin";
        /**
         * 谓词配置集合
         */
        private List<LyGatewayPredicate> predicates;
        /**
         * 过滤器配置集合
         */
        private List<LyGatewayFilter> filters;
    }

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:55
     * @Desc: 谓词配置
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayPredicate {
        /**
         * 路径配置
         */
        private String path;
    }

    /**
     * Created by Intellij IDEA.
     *
     * @Author: linyuxb
     * @Date: 2024/8/14 16:55
     * @Desc: 过滤器配置
     */
    @Getter
    @Setter
    @ToString
    public static class LyGatewayFilter {
        /**
         * 转发之前去掉n层路径 默认去除网关入口路径
         */
        private Integer stripPrefix = 1;
        /**
         * 路径前面会加该配置
         */
        private String prefixPath;
    }

}
